#include <QtGui/QApplication>
#include <QtPlugin>
#include "imy.h"
#include <atlbase.h>

#import "msnmsgr.exe" tlbid(2) no_namespace

inline LPWSTR QstrToBstr(QString& str) { return (LPWSTR)str.data(); }

const QString PluginIMy::s_pluginName = "IMy";
const uint PluginIMy::s_pluginId = qHash(PluginIMy::s_pluginName);

void PluginIMy::Init()
{
}

void PluginIMy::GetPluginId(uint* id)
{
	*id = s_pluginId;
}

void PluginIMy::GetPluginName(QString* name)
{
	*name = s_pluginName;
}

void PluginIMy::GetCatalog(QList<CatItem>* items)
{
	CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
	CComPtr<IMessenger> msgr;
	HRESULT hr = msgr.CoCreateInstance(__uuidof(Messenger), NULL, CLSCTX_LOCAL_SERVER);
	if (FAILED(hr) || !msgr) return;

	try {
		MISTATUS status = msgr->GetMyStatus();
		if (status == MISTATUS_OFFLINE || status == MISTATUS_UNKNOWN) return;

		CComQIPtr<IMessengerContacts> contacts = msgr->GetMyContacts();
		if (!contacts) return;

		long count = contacts->GetCount();
		for (int i = 0; i < count; ++i) {
			CComQIPtr<IMessengerContact> contact = contacts->Item(i);
			if (!contact) continue;

			_bstr_t name = contact->GetFriendlyName();
			_bstr_t account = contact->GetSigninName();
			_bstr_t service = contact->GetServiceId();
			name += L"@" + account;
			_bstr_t filename = account + L"@" + service + L".IMy";
			items->push_back(CatItem(
				QString((QChar*)(BSTR)filename, filename.length()),
				QString((QChar*)(BSTR)name, name.length()),
				s_pluginId,
				GetIcon()));
		}
	} catch (_com_error &err) {
	}
	CoUninitialize();
}

QString PluginIMy::GetIcon()
{
	return qApp->applicationDirPath() + "/plugins/icons/imy.ico";
}

void PluginIMy::LaunchItem(QList<InputData>* id, CatItem* item)
{
	if (!item->fullPath.endsWith(".IMy")) return;

	int delim = item->fullPath.indexOf(L'@');
	delim = item->fullPath.indexOf(L'@', delim+1);
	if (delim < 0) return;

	_bstr_t signinName = QstrToBstr(item->fullPath.left(delim));
	_bstr_t service = QstrToBstr(item->fullPath.mid(delim+1, item->fullPath.length()-delim-5));

	CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
	CComPtr<IMessenger> msgr;
	HRESULT hr = msgr.CoCreateInstance(__uuidof(Messenger), NULL, CLSCTX_LOCAL_SERVER);
	if (FAILED(hr) || !msgr) return;

	try {
		MISTATUS status = msgr->GetMyStatus();
		if (status == MISTATUS_OFFLINE || status == MISTATUS_UNKNOWN) return;

		CComQIPtr<IMessengerContact> contact = msgr->GetContact(signinName, service);
		if (!contact) return;

		CComPtr<IDispatch> wnd = msgr->InstantMessage((IDispatch*)contact);
	} catch (_com_error &err) {
	}
	CoUninitialize();
}

int PluginIMy::msg(int msgId, void* wParam, void* lParam)
{
	switch (msgId) {
		case MSG_INIT:
			Init();
			return 1;
		case MSG_GET_ID:
			GetPluginId((uint*)wParam);
			return 1;
		case MSG_GET_NAME:
			GetPluginName((QString*)wParam);
			return 1;
		case MSG_GET_CATALOG:
			GetCatalog((QList<CatItem>*) wParam);
			return 1;
		case MSG_LAUNCH_ITEM:
			LaunchItem((QList<InputData>*)wParam, (CatItem*)lParam);
			return 1;
	}

	return 0;
}

Q_EXPORT_PLUGIN2(imy, PluginIMy) 
